{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**239. Sliding Window Maximum**  <font color=red size=3>_hard_</font>\n",
    "\n",
    "Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.\n",
    "\n",
    "**Example**\n",
    "\n",
    "```\n",
    "Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3\n",
    "Output: [3,3,5,5,6,7] \n",
    "Explanation: \n",
    "\n",
    "Window position                Max\n",
    "---------------               -----\n",
    "[1  3  -1] -3  5  3  6  7       3\n",
    " 1 [3  -1  -3] 5  3  6  7       3\n",
    " 1  3 [-1  -3  5] 3  6  7       5\n",
    " 1  3  -1 [-3  5  3] 6  7       5\n",
    " 1  3  -1  -3 [5  3  6] 7       6\n",
    " 1  3  -1  -3  5 [3  6  7]      7\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 3, 5, 5, 6, 7]\n",
      "[3, 5, 5, 6, 7]\n"
     ]
    }
   ],
   "source": [
    "def maxSlidingWindow(nums, k):\n",
    "        \"\"\"\n",
    "        1. Brutal Solution: O(kn)\n",
    "        \n",
    "        :type nums: List[int]\n",
    "        :type k: int\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        maximum = [max(nums[:k])]\n",
    "        for i in range(1, len(nums) - k + 1):\n",
    "            cur_max = maximum[-1]\n",
    "            cur_max = 0\n",
    "            for j in range(i, i + k):\n",
    "                cur_max = max(cur_max, nums[j])\n",
    "            maximum.append(cur_max)\n",
    "        return maximum\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    nums = [1,3,-1,-3,5,3,6,7]\n",
    "    print(maxSlidingWindow(nums, 3));  # expect: [3,3,5,5,6,7]\n",
    "    print(maxSlidingWindow(nums, 4));  # expect: [3,3,5,5,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 3, 5, 5, 6, 7]\n",
      "[3, 5, 5, 6, 7]\n"
     ]
    }
   ],
   "source": [
    "import collectionsmaximum = []\n",
    "    q = deque()\n",
    "    for i in range(len(nums)):\n",
    "        if i >= k:\n",
    "            maximum.append(q[0])\n",
    "\n",
    "        while len(q) > 0 and nums[i] >= q[-1]:\n",
    "            q.pop()\n",
    "        q.append(nums[i])\n",
    "\n",
    "    maximum.append(q[0])\n",
    "    return maximum\n",
    "\n",
    "def maxSlidingWindow(nums, k):\n",
    "        \"\"\"\n",
    "        1. O(kn)\n",
    "        \n",
    "        :type nums: List[int]\n",
    "        :type k: int\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        maximum = [max(nums[:k])]\n",
    "        for i in range(1, len(nums) - k + 1):\n",
    "            cur_max = maximum[-1]\n",
    "            if nums[i + k - 1] > cur_max:\n",
    "                cur_max = nums[i + k - 1]\n",
    "            elif nums[i - 1] == cur_max:\n",
    "                cur_max = nums[i]\n",
    "                for j in range(i, i + k):\n",
    "                    cur_max = max(cur_max, nums[j])\n",
    "            maximum.append(cur_max)\n",
    "        return maximum\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    nums = [1,3,-1,-3,5,3,6,7]\n",
    "    print(maxSlidingWindow(nums, 3));  # expect: [3,3,5,5,6,7]\n",
    "    print(maxSlidingWindow(nums, 4));  # expect: [3,3,5,5,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 3, 5, 5, 6, 7]\n",
      "[3, 5, 5, 6, 7]\n"
     ]
    }
   ],
   "source": [
    "from collections import deque\n",
    "\n",
    "def maxSlidingWindow(nums, k):\n",
    "    \"\"\"\n",
    "    :type nums: List[int]\n",
    "    :type k: int\n",
    "    :rtype: List[int]\n",
    "    \"\"\"\n",
    "    maximum = []\n",
    "    q = deque()\n",
    "    for i in range(len(nums)):\n",
    "        if i >= k:\n",
    "            maximum.append(q[0])\n",
    "        while len(q) > 0 and nums[i] >= q[-1]:\n",
    "            q.pop()\n",
    "        q.append(nums[i])\n",
    "    maximum.append(q[0])\n",
    "    return maximum\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    nums = [1, 3, -1, -3, 5, 3, 6, 7]\n",
    "    print(maxSlidingWindow(nums, 3));  # expect: [3,3,5,5,6,7]\n",
    "    print(maxSlidingWindow(nums, 4));  # expect: [3,5,5,6,7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
